import dualLine from './dualLine';
import line from './line';
import bar from './bar';
import scatter from './scatter';
import { errorReport } from '../utils/tools';

const draw = ({ dom, data, chartConfig, type, originType, originIns }) => {
  if (originIns && originType !== type) {
    originIns.destroy();
  }
  const chartIns = window.ThsDataVStandardChart.init(dom);
  try {
    if (!window.ThsDataVStandardChart) {
      throw new Error('ThsDataVStandardChart not found');
    }
    const drawOptions = {
      line,
      dual_line: dualLine,
      bar,
      scatter
    };
    if (drawOptions[type]) {
      return { chartIns: drawOptions[type]({ chartConfig, data, chartIns }), type };
    } else {
      throw new Error(`draw type ${type} not found`);
    }
  } catch (error) {
    errorReport({
      name: `画图错误${error.name}`,
      message: error.message,
      stack: error.stack
    });
  }
};

export default draw;
